package gui.admin;

import gui.AbstractDefensiveDriverPanel;
import gui.InvalidSyncPanelImpl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import objects.School;
import objects.Student;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class AdminDriverDisplayPanelImpl extends AbstractAdminPanelImpl implements AdminDriverDisplayPanel {
	private Table table;
	private Text text;
	private TableColumn studentNumberCol;
	private TableColumn driverNameCol;
	private Button selectBtn;
	private Label searchLbl;
	private Button searchBtn;
	private Label label;
	private Button[] radioButtons = new Button[2];
	
	private List<TableItem> store = new ArrayList<TableItem>();
	private List<Student> students = new ArrayList<Student>();
	private Button syncUsrsBtn;
	private ProgressPanelImpl progress;
	private AbstractDefensiveDriverPanel parent = this;
	
	public AdminDriverDisplayPanelImpl(Display display, Shell shell) {
		super(display, shell);
		
		School school = new School(1);
		try {
			database.getSchool(school);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		progress = new ProgressPanelImpl(display, new Shell());
		progress.setMessage("Updating Users");
		progress.setCaller(this);
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	public void open() {
		// Remove at compile time
//		Shell shell = new Shell();
		// **********************
		
		shell.setSize(800, 600);
		shell.setText("List of Drivers");
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(10, 149, 764, 338);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		studentNumberCol = new TableColumn(table, SWT.NONE);
		studentNumberCol.setWidth(379);
		studentNumberCol.setText("Driver Number");
		
		driverNameCol = new TableColumn(table, SWT.NONE);
		driverNameCol.setWidth(381);
		driverNameCol.setText("Driver Name");
		
		selectBtn = new Button(shell, SWT.NONE);
		selectBtn.setBounds(699, 493, 75, 25);
		selectBtn.setText("Select");
		selectBtn.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				if(table.getSelection().length != 0) {
					AdminDriverProfilePanel window = new AdminDriverProfilePanelImpl(display, shell);
					window.setStudent(students.get(table.getSelectionIndex()));
					dispose();
					window.open();
				}
			}
		});
		
		searchLbl = new Label(shell, SWT.NONE);
		searchLbl.setBounds(10, 118, 72, 25);
		searchLbl.setText("Search:");
		searchLbl.setFont(new Font(display, "Arial", 14, SWT.NONE));
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(88, 118, 346, 25);
		
		radioButtons[0] = new Button(shell, SWT.RADIO);
		radioButtons[0].setBounds(440, 121, 120, 18);
		radioButtons[0].setText("Driver Number");
		radioButtons[0].setFont(new Font(display, "Arial", 12, SWT.NONE));
		
		radioButtons[1] = new Button(shell, SWT.RADIO);
		radioButtons[1].setBounds(580, 121, 105, 18);
		radioButtons[1].setText("Driver Name");
		radioButtons[1].setFont(new Font(display, "Arial", 12, SWT.NONE));
		
		searchBtn = new Button(shell, SWT.NONE);
		searchBtn.setBounds(699, 119, 75, 25);
		searchBtn.setText("Search");
		searchBtn.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				if(radioButtons[0].getSelection()) {
					performUnitSearch(Integer.parseInt(text.getText()));
				} else if(radioButtons[1].getSelection()) {
					performPersonSearch(text.getText());
				}
			}
		});
		
		label = new Label(shell, SWT.WRAP);
		label.setBounds(10, 24, 764, 69);
		label.setText("To select a driver, scroll through the list, highlight their " +
				"student number/name and press the select button.  Or type their unit number or name into " +
				"the search field and press the search button, then press the select button.");
		label.setFont(new Font(display, "Arial", 14, SWT.ITALIC));
		
		syncUsrsBtn = new Button(shell, SWT.NONE);
		syncUsrsBtn.setBounds(10, 493, 75, 25);
		syncUsrsBtn.setText("Sync Users");
		syncUsrsBtn.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				try {
					if(progress.isDisposed()) {
						progress = new ProgressPanelImpl(display, new Shell());
						progress.setCaller(parent);
						progress.setMessage("Updating Users");
					}
					progress.open();
					database.dsm();
					table.removeAll();
					load();
				} catch (SQLException e) {
					InvalidSyncPanelImpl window = new InvalidSyncPanelImpl(display, new Shell());
					window.open();
					e.printStackTrace();
				}
			}
		});
		
		load();

		shell.open();
		shell.layout();
	}

	public void addDriver(String[] driver) {
		if(driver.length == 2) {
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(driver);
			store.add(item);
			table.update();
		}
	}

	public String getDriver() {
		return store.get(table.getSelectionIndex()).getText(1);
	}

	public String getStudentNumber() {
		return store.get(table.getSelectionIndex()).getText(0);
	}
	
	private void performUnitSearch(Integer unitNumber) {
		int index = 0;
		for(TableItem item : store) {
			String toCompare = item.getText(0);
			if(Integer.parseInt(toCompare) == unitNumber) {
				table.select(index);
			}
			index++;
		}
	}
	
	private void performPersonSearch(String person) {
		int index = 0;
		for(TableItem item : store) {
			String toCompare = item.getText(1);
			if(toCompare.trim().equals(person.trim())) {
				table.select(index);
			}
			index++;
		}
	}
	
	private void load() {
		try {
			database.getAllStudents((ArrayList<Student>) students);
		} catch (SQLException e) {
			System.out.println("Unable to load students");
			e.printStackTrace();
		}
		
		for(Student s : students) {
			addDriver(new String[] {Integer.toString(s.getID()), s.getName()});
		}
	}
}
